package me.zhengjie.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.domain.InBoundService;
import me.zhengjie.domain.PlcErrorService;
import me.zhengjie.dto.Resp;
import me.zhengjie.dto.sqlServer.InBoundMission;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/plcError")
@RequiredArgsConstructor
@Api(tags = "机台报警信息同步")
public class PlcErrorController {
    private final InBoundService inBoundService;
    private final PlcErrorService plcErrorService;

    @AnonymousAccess
    @PostMapping("/upload")
    public Resp uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            plcErrorService.processExcelFile(file);
            return Resp.builder()
                    .status(200)
                    .msg("文件上传并处理成功")
                    .build();
        } catch (Exception e) {
            return Resp.builder()
                    .status(500)
                    .msg("处理失败: " + e.getMessage())
                    .build();
        }
    }

    @AnonymousAccess
    @GetMapping(value = "/inboundError")
    public Resp inboundError() {
        if (!ObjectUtil.isEmpty(inBoundService.findNewInBoundMission())) {
            // 查找最新的入库载盘任务
            InBoundMission newInBoundMission = inBoundService.findNewInBoundMission();
            // 发送任务
            inBoundService.sendInBoundTask(newInBoundMission);
            return Resp.builder()
                    .status(200)
                    .msg("处理成功")
                    .build();
        }
        return Resp.builder()
                .status(200)
                .msg("处理失败")
                .build();
    }
}
